package com.zlict.commonelasticsearch.controller;

import com.zlict.commonelasticsearch.domain.AuditLog;
import com.zlict.commonelasticsearch.domain.AuditReportQuery;
import com.zlict.commonelasticsearch.service.ElasticsearchAuditService;
import com.zlict.commonelasticsearch.utils.AuditLogUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 审计日志控制器
 * 
 * @author zlict
 */
@RestController
@RequestMapping("/api/audit")
public class AuditLogController {
    
    @Autowired
    private ElasticsearchAuditService elasticsearchAuditService;
    
    @Autowired
    private AuditLogUtils auditLogUtils;
    
    /**
     * 查询审计日志列表
     */
    @GetMapping("/list")
    public ResponseEntity<Map<String, Object>> list(AuditReportQuery query) {
        try {
            List<AuditLog> list = elasticsearchAuditService.queryAuditLogsForReport(query);
            Map<String, Object> result = new HashMap<>();
            result.put("data", list);
            result.put("total", list.size());
            result.put("success", true);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            Map<String, Object> result = new HashMap<>();
            result.put("success", false);
            result.put("message", "查询失败: " + e.getMessage());
            return ResponseEntity.ok(result);
        }
    }
    
    /**
     * 查询最近几天的审计日志
     */
    @GetMapping("/recent/{days}")
    public ResponseEntity<Map<String, Object>> getRecentLogs(@PathVariable int days) {
        try {
            String logs = elasticsearchAuditService.queryRecentAuditLogs(days);
            Map<String, Object> result = new HashMap<>();
            result.put("data", logs);
            result.put("success", true);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            Map<String, Object> result = new HashMap<>();
            result.put("success", false);
            result.put("message", "查询失败: " + e.getMessage());
            return ResponseEntity.ok(result);
        }
    }
    
    /**
     * 检查Elasticsearch连接状态
     */
    @GetMapping("/health")
    public ResponseEntity<Map<String, Object>> checkHealth() {
        try {
            boolean isAvailable = elasticsearchAuditService.isElasticsearchAvailable();
            String clusterInfo = elasticsearchAuditService.getClusterInfo();
            
            Map<String, Object> result = new HashMap<>();
            result.put("available", isAvailable);
            result.put("clusterInfo", clusterInfo);
            result.put("success", true);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            Map<String, Object> result = new HashMap<>();
            result.put("success", false);
            result.put("message", "健康检查失败: " + e.getMessage());
            return ResponseEntity.ok(result);
        }
    }
    
    /**
     * 记录测试审计日志
     */
    @PostMapping("/test")
    public ResponseEntity<Map<String, Object>> recordTestLog(@RequestBody Map<String, String> request) {
        try {
            String actionType = request.getOrDefault("actionType", "测试操作");
            String actionResult = request.getOrDefault("actionResult", "成功");
            String details = request.getOrDefault("details", "这是一条测试审计日志");
            
            auditLogUtils.recordAuditLog(actionType, actionResult, details);
            
            Map<String, Object> result = new HashMap<>();
            result.put("success", true);
            result.put("message", "测试审计日志记录成功");
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            Map<String, Object> result = new HashMap<>();
            result.put("success", false);
            result.put("message", "记录测试日志失败: " + e.getMessage());
            return ResponseEntity.ok(result);
        }
    }
    
    /**
     * 记录载体操作审计日志
     */
    @PostMapping("/carrier")
    public ResponseEntity<Map<String, Object>> recordCarrierOperation(@RequestBody Map<String, String> request) {
        try {
            String operation = request.getOrDefault("operation", "载体操作");
            String carrierInfo = request.getOrDefault("carrierInfo", "载体信息");
            String result = request.getOrDefault("result", "成功");
            
            auditLogUtils.recordCarrierOperation(operation, carrierInfo, result);
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("message", "载体操作审计日志记录成功");
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> result = new HashMap<>();
            result.put("success", false);
            result.put("message", "记录载体操作日志失败: " + e.getMessage());
            return ResponseEntity.ok(result);
        }
    }
    
    /**
     * 记录登录审计日志
     */
    @PostMapping("/login")
    public ResponseEntity<Map<String, Object>> recordLoginLog(@RequestBody Map<String, String> request) {
        try {
            String username = request.getOrDefault("username", "testuser");
            String actionType = request.getOrDefault("actionType", "登录");
            String actionResult = request.getOrDefault("actionResult", "成功");
            String details = request.getOrDefault("details", "用户登录");
            
            auditLogUtils.recordLoginAuditLog(username, actionType, actionResult, details);
            
            Map<String, Object> result = new HashMap<>();
            result.put("success", true);
            result.put("message", "登录审计日志记录成功");
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            Map<String, Object> result = new HashMap<>();
            result.put("success", false);
            result.put("message", "记录登录日志失败: " + e.getMessage());
            return ResponseEntity.ok(result);
        }
    }
}




